<?php
require 'include/library.inc.php';
require 'include/paypal.inc.php';

# If Empty Session
if (!$_SESSION['course_order']['school_id']) redirect("schools.php");
if (!$_SESSION['course_order']['course_id']) redirect("schools.php");
if (!$_SESSION['course_order']['course_date_id']) redirect("schools.php");

if ($_POST['is_submitted']) {
	
	$doPay = new DoExpressCheckoutPayment("".round($_SESSION['course_order']['registration_fee']*$_SESSION["currency_value"])."");
	$doPay->setNVP("CURRENCYCODE", $_SESSION["currency"]);
	$doPay->setNVP("TOKEN", urlencode($_POST['token']));
    $doPay->setNVP("PAYERID", urlencode($_POST['PayerID'])); 
	$result = $doPay->getResponse(); 
	
} else {
	
	$response = GetExpressCheckoutDetails::request(); 
		
	$sql = 'SELECT
			s.name,
			s.logo,
			c.name AS city,
			c.state,
			d.name AS country,
			d.country_id,
			cou.name AS course_name,
			cou.registration_fee,
			cd.start_date,
			cd.duration,
			cd.tuition
		FROM
			schools AS s,
			cities AS c,
			countries AS d,
			courses AS cou,
			course_dates AS cd
		WHERE
		  	s.school_id = '.$_SESSION['course_order']['school_id'].'
		AND
			s.city_id = c.city_id
		AND
			c.country_id = d.country_id
		AND
			cou.course_id = '.$_SESSION['course_order']['course_id'].'
		AND
			cd.course_date_id = '.$_SESSION['course_order']['course_date_id'].'
		';
	$row = mysql_fetch_assoc(query($sql));
	
}

head("Online Application Form","",101);
?>
	<div id="content">
    
    <?php 
	if (!$_POST['is_submitted'] && $response['ACK'] == 'Success') {
	?>
    	
        <h2>PayPal Authorization Succeeded</h2>
        <br>
        <b>Now you can confirm the payment!</b><br><br>
        
        <form action="<?=HTML_BASE_SSL.$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
        <input type="hidden" name="is_submitted" value="1" />
        <input type="hidden" name="token" value="<?=$_GET['token']?>" />
        <input type="hidden" name="PayerID" value="<?=$_GET['PayerID']?>" />
                
        <div style="width:47%; float:left;">
        	
            <h2>Payer Information</h2>
            
            <div class="padding">
            	<br>
                First Name: <b class="green"><?=$response['FIRSTNAME']?></b><br>
                Last Name: <b class="green"><?=$response['LASTNAME']?></b><br>
                E-mail: <?=urldecode($response['EMAIL'])?><br><br>
                
            </div>
            
        </div>
        <div style="width:47%; float:right;">
            
            <h2>Order Information</h2>
            
            <div class="padding">
            	<br>
                School: <b class="green"><?=$row['name']?></b><br>
                Location: <?=$row['city']?>, <?=$row['state']?>, <?=$row['country']?><br><br>
                Course: <b class="green"><?=$row['course_name']?></b><br><br>
                Tuition: <b class="green"><?=$_SESSION["curr_sim"]?><?=round($row['tuition']*$_SESSION["currency_value"])?></b><br> 
                Start Date: <b class="green"><?=$row['start_date']?></b><br><br>
                Duration: <b class="green"><?=$row['duration']?> weeks</b><br><br>
                
                <hr width="100%"/>
                
                Payment of School Apllication Fee
                <div style="font-size:18px; font-weight:bold; display:inline-block" class="green">
                   <?=$_SESSION["curr_sim"]?><?=round($_SESSION['course_order']['registration_fee']*$_SESSION["currency_value"])?>
                </div>
				
				<br><br>
                
                
            </div>
            
            
            <button style="float:right" type="submit" class="button">Confirm Payment</button>
        	
        
        </div>        
        
        </form>
          
    <?php
	} elseif (!$_POST['is_submitted'] && $response['ACK'] != 'Success') {
		
		unset($_SESSION['course_order']);
		
		
	?>
    	
        <h2>PayPal Authorization is not successful</h2>
        <br>
        <b>There is a problem with your PayPal Authorization! Please review your PayPal account information and then complete the online application again.
        </b>
        <br><br>
        
        <a class="button" href="schools.php">Continue</a>
        
        
    <?php
	} elseif ($_POST['is_submitted'] && $result['ACK'] == 'Success') {
				
		# Update the order status to Paid
		$sql = 'UPDATE orders
				SET
					order_status_id = 2
				WHERE
					order_id = '.$_SESSION['course_order']['order_id'].'
				';
		query($sql);
		
		
		# Mail to User
	    mail( $_SESSION['USER_EMAIL'], 
	          sprintf(USER_ORDER_PAID_SUBJECT, number_pad($_SESSION['course_order']['order_id'],5)),
	          sprintf(USER_ORDER_PAID_MESSAGE, number_pad($_SESSION['course_order']['order_id'],5)), 
	          HTML_HEADERS."From: ESL Traveler <".NO_REPLY_EMAIL.">");
	          
	    # Mail to Admin
	    mail( ADMINISTRATOR_EMAIL,
	          sprintf(ADMIN_ORDER_PAID_SUBJECT, number_pad($_SESSION['course_order']['order_id'],5)), 
	          sprintf(ADMIN_ORDER_PAID_MESSAGE, number_pad($_SESSION['course_order']['order_id'],5)), 
	          HTML_HEADERS."From: ESL Traveler <".NO_REPLY_EMAIL.">");
		
	?>
		        
        <h2>Thank you for your application</h2>
        <br>
        <b class="green">Your transaction is successful!</b><br><br>
        
        You will receive a detailed instructions how to continue your application process in the next 3-5 business days on your e-mail.
        
        <br><br>
        
        <a class="button" href="myprofile.php">Continue</a>
		
		
		<!--Begin clixGalore Code -->
		<script language="javascript">
		var arrParams = location.search.split("?")[1].split("&");
		var SV   = '';
		var OID  = '';
		var temp = '';
		
		if(location.search.indexOf("?")!=-1) 
		{
		  if (arrParams)   
		  {
			for (i=0;i<arrParams.length;i++) 
			{
			  var param=arrParams[i].split("=");
			  temp = param[0];
		
			  if(temp.toLowerCase()=="amt")
			  {
				 SV = param[1]
			  }
			  if(temp.toLowerCase()=="tx")
			  {
				 OID = param[1]
			  }
			}
		  }
		}
		
		document.write ('<img src="https://www.clixGalore.com/AdvTransaction.aspx?AdID=14537&SV=' + SV + '&OID=' + OID + '" height="0" width="0" border="0">');
		</script>
		<!--End clixGalore Code -->
		
		<!--Pampanetwork Tracking pixel-->
		<img src="https://my.pampanetwork.com/scripts/sale.php?AccountId=f33d1e25&TotalCost=<?=$_SESSION['course_order']['registration_fee']?>&OrderID=<?=$_SESSION['course_order']['order_id']?>&ProductID=esltraveler&ActionCode=sale" width="1" height="1" >
                    
	<?php
		
		unset($_SESSION['course_order']);
		
	} elseif ($_POST['is_submitted'] && $result['ACK'] == 'Failure') {
		
		# Update the order status to Failed
		$sql = 'UPDATE orders
				SET
					order_status_id = 6
				WHERE
					order_id = '.$_SESSION['course_order']['order_id'].'
				';
		query($sql);
		
		
		unset($_SESSION['course_order']);
	?> 
    	
    	<h2>PayPal Payment is not successful</h2>
        <br>
        <b class="red">There is a problem with your PayPal payment!</b>
        <br><br>
        
        Please review your PayPal account information and then complete the online application again. 
        
        <br><br>
        
        <a class="button" href="schools.php">Continue</a>
    	
    <?php 
	}
	?>
    </div>
            
<?php foot(); ?>